import Vue from 'vue'
import Vuex from 'vuex'
import homeSwiper from './home/home'
import Class from './class/class'
import GetToken from './token/Token'
import VuexPersistence from "vuex-persist";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    arr: []
  },
  mutations: {
    // 添加购物车
    shopping(state, item) {

      // let falg = false
      state.arr.push({
        item,
        done: false
      })
      // state.arr.forEach(i=>{
      //   if(i.item.id==item.id){
      //     i.item.num=item.num+i.item.num
      //     falg=true
      //   }
      // })
      // if(falg==false){
      //   state.arr.push({
      //     item,
      //     done:false
      //   })
      // }
    },
    che(state) {
      state.arr.done = !state.arr.done
    },
    // val(state,num){
    //   state.arr.item.num = num
    // }
    // 全选按钮
    chooseall(state,val){
      // console.log(val)
      state.arr.forEach(i=>{
        i.done=val
      })
    },
  },
  actions: {},
  modules: {
    homeSwiper,
    Class,
    GetToken,
  },
  getters: {
    // 选中的商品计算总价
    allprice(state) {
      let sum = 0;
      state.arr.forEach((i) => {
        if (i.done == true) {
          sum += i.item.jiage * i.item.num
        }
      })
      return sum
    }
  },
  plugins: [
    new VuexPersistence({
      storage: window.localStorage,
    }).plugin,
  ],
})